/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MessageHandler;

import Core.MessageBuilder;
import java.util.Iterator;
import java.util.List;
import org.openymsg.network.Session;
import org.openymsg.network.SessionState;
import org.openymsg.network.Status;
import org.openymsg.network.YahooGroup;
import org.openymsg.network.YahooUser;

/**
 *
 * @author Mr Bao
 */
public class LogInMessageHandler implements IMessageHandler {

    public Object Process(String message, Session session) {
        try {
            String[] userInfo = message.split(MessageBuilder.TWO);
            String username = userInfo[0].substring(userInfo[0].indexOf(MessageBuilder.ONE) + 1).trim();
            String password = userInfo[1].trim();
            boolean invisible = Boolean.parseBoolean(userInfo[2]);
            if (invisible) {
                session.setStatus(Status.INVISIBLE);
            }
            session.login(username, password);
            List<YahooGroup> group = session.getGroups();
            for (YahooGroup yahooGroup : group) {
                System.out.println("Group: " + yahooGroup.getName());
                for (YahooUser yahooUser : yahooGroup.getUsers()) {
                    System.out.println("### group member: " + yahooUser.getNickName());
                }
            }
            if (session.getSessionStatus() == SessionState.LOGGED_ON) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
}
